<template>
  <div>
    <!---授权窗口开始---->
    <el-dialog
        title="分配权限"
        :visible.sync="permitDialogVisible"
        width="30%">

      <el-tree
          ref="menuTree"
          :data="treeData"
          show-checkbox
          node-key="mid"
          default-expand-all
          :props="defaultProps"
          :default-checked-keys="defaultCheckedKeys"
      >
      </el-tree>

      <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="savePermits()">确 定</el-button>
            </span>
    </el-dialog>
    <!---授权窗口结束---->


    <el-row>
      <el-col :span="2" style="padding:5px; ">
        <el-button type="primary" @click="showWin()">新增</el-button>

      </el-col>
      <el-col :span="5" style="padding:5px; ">
        <el-input v-model="keyword" placeholder="请输入内容"></el-input>
      </el-col>
      <el-col :span="17" style="padding:5px; ">
        <el-button type="primary" @click="search()">搜索</el-button>
      </el-col>

    </el-row>

    <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="30%"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="角色名称" prop="rname">
          <!--                    <input type="text" v-model="form.head">-->
          <el-input v-model="form.rname"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
      </span>
    </el-dialog>

    <el-table
        :data="tableData"
        style="width: 100%">
      <el-table-column
          prop="rid"
          label="角色编号"
          width="180">
      </el-table-column>
      <el-table-column
          prop="rname"
          label="角色名称"
      >
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="150">
        <template slot-scope="scope">
          <el-button @click="showWin(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="showPermitWin(scope.row)" type="text" size="small">分配权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        @current-change="currentChange"
        layout="prev, pager, next"
        :page-size="pageSize"
        :current-page="currentPage"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  created() {
    //界面加载完成往后台发送ajax请求 获取用户数据
    this.loadData();
    //发送请求 查询所有的菜单
    this.$http.post("role/listAllMenus").then(res => {
      console.info("所有菜单数据", res);
      this.treeData = res.data;
    });
  },
  name: "RoleVue",
  data() {
    return {
      //树形组件的默认选中项
      defaultCheckedKeys: [],
      //选中的角色的id
      rid: 0,
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'mname'
      },
      //总条数,
      total: 100,
      //当前页
      currentPage: 1,
      //每页多少条
      pageSize: 10,
      //搜索的关键字
      keyword: "",
      dialogVisible: false,
      permitDialogVisible: false,
      title: "新增角色",
      tableData: [],
      form: {
        rname: ""
      },
      //声明验证规则
      rules: {
        //角色名的校验规则
        rname: [
          {required: true, message: '请输入角色名', trigger: 'blur'},
          {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    //保存角色权限信息
    savePermits() {
      //获取角色id
      console.info("要保存权限的角色id是", this.rid);
      //获取选中的菜单id 调用方法要先获取到组件  获取全选中状态的 父节点和子节点
      let keysChecked = this.$refs.menuTree.getCheckedKeys();
      let keysHalfChecked = this.$refs.menuTree.getHalfCheckedKeys();
      console.info("全选中状态菜单项的mid", keysChecked);
      console.info("半选中状态菜单项的mid", keysHalfChecked);
      let array = [];
      //把keysChecked数组中的元素全部放到array中
      for (let i = 0; i < keysChecked.length; i++) {
        array.push(keysChecked[i]);
      }
      //把keysHalfChecked数组中的元素全部放到array中
      for (let i = 0; i < keysHalfChecked.length; i++) {
        array.push(keysHalfChecked[i]);
      }
      console.info("array", array);
      //把array转成json字符串
      let mids = JSON.stringify(array);
      console.info("mids", mids);
      //往后台发送ajax请求 带上角色id参数和选中的菜单id 参数
      this.$http.post("role/savePermits", {rid: this.rid, mids}).then(res => {
        console.info("保存权限的返回值是", res);
        if (res.data.success) {
          this.permitDialogVisible = false;
        }
      });

    },
    //加载数据
    loadData() {
      //往后台发送ajax请求
      this.$http.post("role/listAll", {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword
      }).then(res => {
        console.info("后台返回的数据", res);
        this.tableData = res.data.list;
        this.total = res.data.total;
      });
    },
    //弹出窗口
    showWin(row) {
      //如果是编辑
      if (row) {
        this.form = Object.assign({}, row);
        this.title = "编辑角色";
      } else {
        this.title = "新增用户";
        this.form = {
          rname: ""
        }

      }
      this.dialogVisible = true;
    },
    //保存或者编辑用户信息
    saveOrUpdate() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          //往后台发送ajax请求
          this.$http.post("role/saveOrUpdate", this.form).then(res => {
            console.info("后台返回的数据", res);
            if (res.data.success) {
              this.loadData();
              this.dialogVisible = false;
            } else {
              this.$message.error(res.data.error);
            }
          });
        } else {
          return false;
        }
      });
    },
    //搜索
    search() {
      this.loadData();
    },
    currentChange(currentPage) {
      this.currentPage = currentPage;
      this.loadData();
    },
    //弹出授权窗口
    showPermitWin(row) {
      this.permitDialogVisible = true;
      this.rid = row.rid;
      //往后台发送请求 查询当前角色的权限信息
      this.$http.post("role/listMidsByRid", {rid: row.rid}).then(res => {
        console.info("当前角色的菜单数据", res);
        //全部取消选中
        this.defaultCheckedKeys = [];
        //设置选中项
        //设置树形组件选中指定的菜单项
        this.$refs.menuTree.setCheckedKeys(res.data);

        // for(let i = 0;i<res.data.length;i++){
        //     this.defaultCheckedKeys.push(res.data[i]);
        // }
        // console.info("数组",this.defaultCheckedKeys);
      });
    }
  }
}
</script>

<style scoped>
.el-upload1 {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 200px;
}

.el-upload1:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>